package model;

//~--- non-JDK imports --------------------------------------------------------

import SessionFactory.SessionFactoryUtil;
import dto.JfMembers;
import dto.JfMembersHome;
import dto.ModPrev;
import dto.ModPrevHome;
import dto.Sections;
import dto.SectionsHome;
import dto.Themes;
import dto.ThemesHome;
import dto.Topics;
import dto.TopicsHome;

//~--- JDK imports ------------------------------------------------------------

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.struts.upload.FormFile;
import org.hibernate.SessionFactory;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

public class AdminControlPanel {
    private SectionsHome sh;
    private TopicsHome   th;
    JfMembersHome memhome;
	ModPrevHome  modhome;
	SectionsHome sechome;
    public AdminControlPanel() {
        sh = new SectionsHome();
        th = new TopicsHome();
    }

    public List getForums() {
        List sections = sh.getAllSections();

        return sections;
    }

    public Sections getSection(int id) {
        Sections section = sh.findById(new Integer(id));

        return section;
    }

    public Topics getTopic(int id) {
        Topics topic = th.findById(new Integer(id));

        return topic;
    }

    public void createSection(String name, Sections parent, int isPublic) {
        Sections section = new Sections();

        section.setSectionname(name);
        section.setIspublic(new Integer(isPublic));
        section.setSections(parent);
        sh.persist(section);
    }

    public void deleteSection(int id) {
        Sections section = new Sections();

        section.setSectionid(new Integer(id));
        sh.delete(section);
    }

    public void setSectionPublicity(int id, int ispublic) {
        Sections section = getSection(id);

        section.setIspublic(new Integer(ispublic));
        sh.attachDirty(section);
    }

    public void setTopicPublicity(int id, int ispublic) {
    	th.startNew();
        Topics topic = getTopic(id);

        topic.setIspublic(new Integer(ispublic));
        th.attachDirty(topic);
        th.CommitTrans();
    }
    
    
    public void backUpDataBase() throws Exception 
	{
		System.out.println("Running the batch script");
		Runtime.getRuntime().exec("cmd /c C:\\export.bat");
		Thread.sleep(3000);
		System.out.println("Finished running the batch script");
		
	}

	public void restoreDatabase() throws Exception 
	{
		System.out.println("Running the batch script");
		Runtime.getRuntime().exec("cmd /c C:\\import.bat");
		Thread.sleep(3000);	
		System.out.println("Finished running the batch script");
		
	}
        public Vector<Sections> getParentTree(Sections section) {              
                Vector<Sections> parents=new Vector<Sections>();               
                do{
                    System.out.println("testing........................");
                     parents.insertElementAt(section,0);
                     section=section.getSections();
                }while(section!=null);
                return parents;
	}

	
	public Vector<Sections> getSectionsTree() {
SectionsHome sh=new SectionsHome();
		
		sh.startNew();
		List sects=sh.getAllSections();
		Vector<Sections> total=new Vector<Sections>();
		String str="";
		
		Vector<Sections> fin=getSections(sects, str, total);
		
		for(Sections s:fin){
			System.out.println("//////////// "+s.getSectionname());
		}
		return fin;
	}
	
public Vector getSections(List sections,String str,Vector<Sections> total) {
		
		for(int i=0;i<sections.size();i++){
			Sections sect=(Sections)sections.get(i);
			sect.setSectionname(str+sect.getSectionname());
			total.add(sect);
			List sects=new ArrayList<Sections>(sect.getSectionses());
			if(sects.size()!=0){
				String s=new String(str+"+");
				getSections(sects, s, total);
			}
		}
		return total;
	}

public List getThemes() {
	ThemesHome theme=new ThemesHome();	
	theme.startNew();
	List result=theme.getAllThemes();
	theme.finish();
	return result;
}

public Themes getTheme(String themename) {
	ThemesHome theme=new ThemesHome();
	Themes t=new Themes();
	t.setThemename(themename);
	theme.startNew();
	t=theme.findByExample(t).get(0);
	theme.finish();
	return t;
}

public boolean addTheme(String themename, FormFile file) {
	boolean check=true;
    ThemesHome themehome=new ThemesHome();
    Themes theme=new Themes();
    FileOutputStream outputStream = null;	
	try 
	{	    		
		outputStream = new FileOutputStream(new File("D:/ITI/ME/labs/project/web/css/"+file.getFileName()));
		outputStream.write(file.getFileData());
		theme.setThemename(themename);
		theme.setThemepath("D:/ITI/ME/labs/project/web/css/"+file.getFileName());
		themehome.startNew();
		themehome.persist(theme);
		check=themehome.commitTrans();
	}catch(Exception e){
		e.printStackTrace();
		check=false;
	}
	finally 
	{
		if (outputStream != null) 
		{try{
			outputStream.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		}
	}
	
	return check;
}


public boolean deleteTheme(String themename) {
    boolean check=true;
	ThemesHome theme=new ThemesHome();
	Themes t=new Themes();
	t.setThemename(themename);
	theme.startNew();
	t=theme.findByExample(t).get(0);
        File file=new File(t.getThemepath());
        file.delete();
	theme.delete(t);
	check=theme.commitTrans();
	return check;
        
}

    public void finish() {
        sh.finish();
    }

    public void startNew() {
        sh.startNew();
    }

    public boolean save() {
        boolean check = sh.commitTrans();
        sh.finish();

        return check;
    }
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * user management
     */
    
    public Vector<JfMembers> getAllUsers() {
		memhome = new JfMembersHome();
		return  memhome.getAllUsers1();
	}

	
	public Vector<JfMembers> getModerators() {
		System.out.println("enter admin");
		memhome = new JfMembersHome();
		return memhome.getModerators();
	}
	
	public void promoteUser(Integer id,Integer type,Integer[] sectionid) {
            sh=new SectionsHome();
                modhome=new ModPrevHome();
                memhome = new JfMembersHome();
                sh.startNew();
            if(sectionid==null){
                
		memhome.promoteUser(id, type);
            }else{
                JfMembers member=memhome.findById(id);
                member.setUserprev(type);
                memhome.attachDirty(member);
                for(Integer sectid:sectionid){
                    Sections sect=sh.findById(sectid);
                    ModPrev prev=new ModPrev();
                    prev.setJfMembers(member);
                    prev.setSections(sect);
                    modhome.persist(prev);
                }
                sh.commitTrans();
                sh.finish();
            }
		
		
	}
public void editModerator(JfMembers user, Integer type,Vector<Sections> Sections) {
		
		memhome = new JfMembersHome();
		memhome.editModerator(user, type, Sections);
		
	}

public void demoteModerator(Integer id) {
	memhome = new JfMembersHome();
        modhome=new ModPrevHome();
        SessionFactoryUtil.startNew();
	boolean check=memhome.demoteModerator(id);
          System.out.println("check ==== "+check);
        if(check)
        {
            modhome.deletemod(id);
        }
        SessionFactoryUtil.commitTrans();
//        System.out.println("it's ok //////////////////////////  check: "+check);
//        if(check){
//            System.out.println("it's ok //////////////////////////  nice work");
//            modhome.startNew();
//            ModPrevHome mphome = new ModPrevHome();
//            JfMembers member=memhome.findById(id);
//            ModPrev modprev=new ModPrev();
//            System.out.println("it's ok //////////////////////////  user: "+member.getMemberId());
//            modprev.setJfMembers(member);
//            List<ModPrev> prevs=modhome.findByExample(modprev);
//            System.out.println("it's ok //////////////////////////  list size "+prevs.size());
//            for(ModPrev prev: prevs){
//                System.out.println("it's ok //////////////////////////  prev num "+prev.getModPrevId());
//                 modhome.delete(modprev);
            
           
//            	boolean check=memhome.demoteModerator(id);
//            modhome.close();
           
        
	
}

public Vector<Sections> getAllSections() {
	 sechome = new SectionsHome();
	return sechome.getAllSections1();
}
    
    
}


//~ Formatted by Jindent --- http://www.jindent.com
